package com.drug.service.impl;
import com.drug.domain.Result;
import com.drug.domain.User;
import com.drug.domain.UserR;
import com.drug.domain.UserRole;
import com.drug.mapper.UserMapper;
import com.drug.mapper.UserRoleMapper;
import com.drug.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private  UserMapper userMapper;
    private UserR userR;

    /**
     *登录
     * @param map
     * @return
     */
    @Override
    public Result login(Map map, HttpServletRequest request) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        User user = new User();
        user.setuName(username);
        user.setuPassword(password);
        Result result = new Result();
        String check = (String) map.get("check");
        String code2 = (String) map.get("code2");
        if (check!=null&&check!=""){
            String s = check.toLowerCase();
            String code = code2.toLowerCase();
            List<User> userList = userMapper.selectUser();
            for (int i = 0; i < userList.size(); i++) {
                User user1 = userList.get(i);
                String name = user1.getuName();
                String pass = user1.getuPassword();
                if (code.equals(s)){
                    if (name.equals(username)&&pass.equals(password)){
                        request.getSession().setAttribute("CHECKCODE_USERNAME",username);
                        result.setMsg("登录成功");
                        result.setCode(1);
                        break;
                    }else {
                        result.setMsg("用户名或密码错误");
                        result.setCode(0);
                    }
                }else {
                    result.setMsg("验证码错误");
                }
            }
        }else {
            result.setMsg("请输入验证码!!");
        }
        return result;
    }

    /**
     * 注册
     * @param
     */
    @Override
    public Result registerUser(UserR userR) {
        String uName = userR.getUName();
        Integer rId = userR.getRId();
        String password = userR.getUPassword();
        String uCompany = userR.getUCompany();
        String uAdress = userR.getUAdress();
        String postalcode = userR.getUPostalcode();
        String uContacts = userR.getUContacts();
        String uPhone = userR.getUPhone();
        String email = userR.getUEmail();
        User user1 = new User();
        user1.setuName(uName);
        user1.setuPassword(password);
        user1.setuAdress(uAdress);
        user1.setuCompany(uCompany);
        user1.setuPostalcode(postalcode);
        user1.setuPhone(uPhone);
        user1.setuContacts(uContacts);
        user1.setuEmail(email);
        Integer integer = userMapper.addUser(user1);
        Integer uid = user1.getuId();
        UserRole userRole = new UserRole();
        userRole.setUId(uid);
        userRole.setRId(rId);
        System.err.println(userRole);
        int i = userMapper.registerUser(userRole);
        Result result = new Result();
        if (i==1){
            result.setMsg("注册成功");
        }else{
            result.setMsg("注册失败");
        }
        return result;
    }

    @Override
    public Result findUsername(String val) {
        Result result = new Result();
      String[] username=  userMapper.selectUsername();
        for (int i = 0; i < username.length; i++) {
            if (username[i].equals(val)){
                result.setMsg("用户名已存在");
                result.setCode(0);
                break;
            }else {
                result.setMsg("用户名可用");
                result.setCode(1);

            }
        }
        return result;
    }


}
